/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminGroup;

import com.ControlEscolar.Beans.Professor;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Services.ProfessorService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author kossel
 */
public class AdminProf extends MappingDispatchAction{
    private static final String MODIFY="m";
    ArrayList results=new ArrayList();
    ProfessorService service = new ProfessorService();
   
    public ActionForward viewAll (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
         results=service.getProfessorList();
         HttpSession session = request.getSession();
          session.setAttribute("professorList", results);
        return mapping.findForward("success");
    }
    
        public void modifyProf (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          Professor prof = new Professor();
          ProfForm profForm = (ProfForm)form;
          prof.setId_Professor(Integer.parseInt(profForm.getiD_Professor()));
          prof.setProfessor_Name(profForm.getProfessor_Name());
          prof.setProfessor_Tel(profForm.getProfessor_Tel());
          prof.setState(profForm.getState());
          profForm.setAction("");
          try{
          service.updateProfessor(prof);
          response.getWriter().print("success");
          }catch(ServiceExp ex){
            response.getWriter().print("No se puede modificar este profesor, Verifique sus datos");
          }
          
         }

        public ActionForward getProf(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
        String profKey=request.getParameter("id");
         Professor prof = new Professor();
         prof = service.getProfessor(Integer.parseInt(profKey));
         ProfForm profForm = (ProfForm)form;
         profForm.setAction(MODIFY);
         profForm.setiD_Professor(profKey);
         profForm.setProfessor_Name(prof.getProfessor_Name());
         profForm.setProfessor_Tel(prof.getProfessor_Tel());
         profForm.setState(prof.getState());
         return mapping.findForward("success");

    }

      public void addProf(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          ProfForm profForm = (ProfForm) form;
          Professor prof = new Professor();
          prof.setProfessor_Name(profForm.getProfessor_Name());
          prof.setProfessor_Tel(profForm.getProfessor_Tel());
          System.out.println(profForm.getState());
          prof.setState(profForm.getState());
          try { 
             service.createProfessor(prof);
          response.getWriter().print("success");
     }catch(ServiceExp ex){
             response.getWriter().print("Ya existe este professor");
        }

    }

}
